package com.example.jones.test;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.util.CookbookImage;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListAdepter extends BaseAdapter {

    Handler handler;
	private JSONArray jsonArray;
	ArrayList<HashMap<String, Object>> list;
    private Context ctx;
    
	public ListAdepter(Handler handler,JSONArray jsonArray,Context ctx){
		this.handler=handler;
		this.jsonArray = jsonArray;
		this.ctx = ctx;
	}
	public int getCount() {
		return jsonArray.length();
	}

	public Object getItem(int position) {
		return jsonArray.optJSONObject(position);
	}

	public long getItemId(int position) {
		try{
			// 返回物品的ID
			return ((JSONObject)getItem(position)).getInt("id");
		}
		catch (JSONException e){
			e.printStackTrace();
		}
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		System.out.println("ListAdepter : getView:position:"+position+"jsonArray.length():"+jsonArray.length());
		convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.lv_list_item, null);
	
		ImageView lv_collect=(ImageView) convertView.findViewById(R.id.lv_collect);
		ImageView lv_commect=(ImageView) convertView.findViewById(R.id.lv_comment);
		ImageView  cbImg=(ImageView) convertView.findViewById(R.id.cbImg);//菜谱图片
		TextView  title=(TextView) convertView.findViewById(R.id.title);//菜名
		//TextView  iv_material=(TextView) convertView.findViewById(R.id.iv_material);
		TextView  lv_collect_num=(TextView) convertView.findViewById(R.id.lv_collect_num);
		TextView  lv_comment_num=(TextView) convertView.findViewById(R.id.lv_comment_num);
		
		try {	
			CookbookImage cbimg=new CookbookImage();
			String dishImage=((JSONObject)getItem(position)).getString("dishImage");
			cbImg.setImageBitmap(cbimg.getmybitmap(dishImage));
			
			title.setText(((JSONObject)getItem(position)).getString("dishName"));
//			iv_material.setText(((JSONObject)getItem(position)).getString(""));
			lv_collect_num.setText(((JSONObject)getItem(position)).getString("dishHits"));
			lv_comment_num.setText(((JSONObject)getItem(position)).getString("shareNum"));
			
		} catch (JSONException e) {
			System.out.println("########HotAdepter：getView 获取JSONArray元素的属性出错！");
			e.printStackTrace();
		}
		
		lv_commect.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handler.sendEmptyMessage(0);
			}
		});
		
		lv_collect.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				handler.sendEmptyMessage(1);
			}
		});
		
		cbImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in=new Intent();
				try {
					in.putExtra("dishImage", ((JSONObject)getItem(position)).getString("dishImage"));
					in.putExtra("dishName", ((JSONObject)getItem(position)).getString("dishName"));
					in.putExtra("cbID", ((JSONObject)getItem(position)).getString("cookbookID"));

				} catch (JSONException e) {
					e.printStackTrace();
				}
				in.setClass(ctx, DtlActivity.class);
				ctx.startActivity(in);
			}
		});
		return convertView;
	} 
}
